<template>
  <div>
    <sticky class-name="sub-navbar white">
      <el-button style="margin:5px 10px;float:left" type="text" icon="el-icon-arrow-left" @click="handleReturn" >返回</el-button>
      <div>
        <!-- <el-button :loading="loading" style="margin-left: 10px;" type="primary" @click="handleSave">保存</el-button> -->
        <!-- <confirm-poptip v-if="canDelete" :loading="loading" title="确定删除？" style="margin-left: 10px;" type="danger" @on-ok="handleDelete">删除
        </confirm-poptip> -->
      </div>
    </sticky>

    <div style="margin:10px">
      <el-row >
        <el-col :span="12" :xs="24" :md="12" :lg="12">
          <el-card style="margin:10px">
            <div slot="header" class="clearfix">
              <span>资助人信息</span>
            </div>
            <el-form ref="formStudent" :model="form" :rules="rules" label-width="100px" size="mini">

              <el-form-item :style="formEdit.name?'margin-bottom:0px':''" label="姓名">
                <label v-if="!formEdit.name">{{ form.name }} {{ form.anonymous?"(匿名)":'' }}</label>
                <el-form v-else ref="form_name" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="name">
                    <el-input v-model="form.name" :max-length="20" style="width:calc(100% - 120px)" @keyup.enter.native="handleEditAttrOk('name')"/>
                    <el-checkbox v-model="form.anonymous" style="margin-right:0px;">匿名</el-checkbox>
                    <el-button v-if="formEdit.name" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('name')" />
                    <el-button v-if="formEdit.name" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('name')" />
                  </el-form-item>
                </el-form>

                <el-button v-if="!formEdit.name" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('name')" />
              </el-form-item>
              <el-form-item :style="formEdit.nickname?'margin-bottom:0px':''" label="昵称">
                <label v-if="!formEdit.nickname">{{ form.nickname }}</label>

                <el-form v-else ref="form_nickname" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="nickname">
                    <el-input v-model="form.nickname" :max-length="20" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('nickname')"/>

                    <el-button v-if="formEdit.nickname" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('nickname')" />
                    <el-button v-if="formEdit.nickname" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('nickname')" />
                  </el-form-item>
                </el-form>

                <el-button v-if="!formEdit.nickname" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('nickname')" />
              </el-form-item>

              <el-form-item :style="formEdit.gender?'margin-bottom:0px':''" label="性别" >
                <label v-if="!formEdit.gender" >{{ form.gender | genderFilter }}</label>

                <el-form v-else ref="form_gender" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="gender">
                    <el-select v-model="form.gender" style="width:calc(100% - 50px)">
                      <el-option v-for="item in genders" :key="item.code" :label="item.name" :value="item.code"/>
                    </el-select>

                    <el-button v-if="formEdit.gender" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('gender')" />
                    <el-button v-if="formEdit.gender" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('gender')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.gender" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('gender')" />
              </el-form-item>

              <el-form-item :style="formEdit.birthday?'margin-bottom:0px':''" label="生日">
                <label v-if="!formEdit.birthday">{{ form.birthday }}</label>
                <el-form v-else ref="form_birthday" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="birthday">
                    <el-date-picker v-model="form.birthday" type="date" style="width:calc(100% - 50px)" />

                    <el-button v-if="formEdit.birthday" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('birthday')" />
                    <el-button v-if="formEdit.birthday" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('birthday')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.birthday" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('birthday')" />
              </el-form-item>

              <el-form-item :style="formEdit.area?'margin-bottom:0px':''" label="所在地区">
                <label v-if="!formEdit.area">{{ form.area }}</label>
                <el-form v-else ref="form_area" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="area">
                    <el-input v-model="form.area" :max-length="20" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('area')" />

                    <el-button v-if="formEdit.area" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('area')" />
                    <el-button v-if="formEdit.area" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('area')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.area" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('area')" />
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col :span="12" :xs="24" :md="12" :lg="12">
          <el-card style="margin:10px">
            <div slot="header" class="clearfix">
              <span>联系方式</span>
            </div>
            <el-form ref="formContact" :model="form" :rules="rules" label-width="100px" size="mini">
              <el-form-item :style="formEdit.contactPhone?'margin-bottom:0px':''" label="电话">
                <label v-if="!formEdit.contactPhone">{{ form.contactPhone }}</label>
                <el-form v-else ref="form_contactPhone" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="contactPhone">
                    <el-input v-model="form.contactPhone" :max-length="20" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('contactPhone')"/>

                    <el-button v-if="formEdit.contactPhone" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('contactPhone')" />
                    <el-button v-if="formEdit.contactPhone" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('contactPhone')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.contactPhone" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('contactPhone')" />
              </el-form-item>
              <el-form-item :style="formEdit.contactWechat?'margin-bottom:0px':''" label="微信" >
                <label v-if="!formEdit.contactWechat">{{ form.contactWechat }}</label>
                <el-form v-else ref="form_contactWechat" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="contactWechat">
                    <el-input v-model="form.contactWechat" :max-length="20" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('contactWechat')"/>

                    <el-button v-if="formEdit.contactWechat" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('contactWechat')" />
                    <el-button v-if="formEdit.contactWechat" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('contactWechat')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.contactWechat" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('contactWechat')" />
              </el-form-item>
              <el-form-item :style="formEdit.contactEmail?'margin-bottom:0px':''" label="Email">
                <label v-if="!formEdit.contactEmail">{{ form.contactEmail }}</label>
                <el-form v-else ref="form_contactEmail" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="contactEmail">
                    <el-input v-model="form.contactEmail" :max-length="20" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('contactEmail')"/>

                    <el-button v-if="formEdit.contactEmail" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('contactEmail')" />
                    <el-button v-if="formEdit.contactEmail" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('contactEmail')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.contactEmail" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('contactEmail')" />
              </el-form-item>
              <el-form-item :style="formEdit.contactAddress?'margin-bottom:0px':''" label="地址" >
                <label v-if="!formEdit.contactAddress">{{ form.contactAddress }}</label>
                <el-form v-else ref="form_contactAddress" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="contactAddress">
                    <el-input v-model="form.contactAddress" :max-length="50" style="width:calc(100% - 50px)" @keyup.enter.native="handleEditAttrOk('contactAddress')"/>

                    <el-button v-if="formEdit.contactAddress" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('contactAddress')" />
                    <el-button v-if="formEdit.contactAddress" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('contactAddress')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.contactAddress" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('contactAddress')" />
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>

      <el-row>
        <el-card style="margin:10px">
          <div slot="header" class="clearfix">
            <span>备注</span>
          </div>
          <div>
            <el-form ref="formDescription" :model="form" :rules="rules" size="mini">
              <el-form-item :style="formEdit.remark?'margin-bottom:0px':''" label="" >
                <div v-if="!formEdit.remark" style="width:calc(100% - 50px);float:left">
                  <div v-for="(item, index) in getLines(form.remark)" :key="index">
                    {{ item }}
                  </div>
                </div>
                <el-form v-else ref="form_remark" :model="form" :rules="rules" size="mini">
                  <el-form-item prop="remark">
                    <el-input v-model="form.remark" :max-length="200" autosize type="textarea" style="width:calc(100% - 50px)"/>

                    <el-button v-if="formEdit.remark" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-close" @click="handleEditAttrCancel('remark')" />
                    <el-button v-if="formEdit.remark" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-check" @click="handleEditAttrOk('remark')" />
                  </el-form-item>
                </el-form>
                <el-button v-if="!formEdit.remark" style="margin:0px 5px;float:right" type="text" size="mini" icon="el-icon-edit" @click="handleEditAttr('remark')" />
              </el-form-item>
            </el-form>

          </div>
        </el-card>
      </el-row>
    </div>

  </div>
</template>

<script>
import { getInfo, update } from '@/api/volunteers'
import { getGenders } from '@/api/public'
import waves from '@/directive/waves' // 水波纹指令
import elDragDialog from '@/directive/el-dragDialog'
import Sticky from '@/components/Sticky'
import { getLines, parseTime } from '@/utils'

const sponsorTypesKeyValue = {}
const genderKeyValue = {}
export default {
  name: 'VolunteersDetail',
  directives: {
    waves, elDragDialog
  },
  components: { Sticky },
  filters: {
    sponsorTypeFilter(sponsorType) {
      return sponsorTypesKeyValue[sponsorType]
    },
    genderFilter(gender) {
      return genderKeyValue[gender]
    }
  },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      controllerName: 'volunteers',
      loading: false,
      url: '/student/list',

      genders: [],
      sponsorTypes: [],
      form: {},
      formSource: {},
      formEdit: {
        name: false,
        gender: false,
        nickname: false,
        anonymous: false,
        area: false,
        birthday: false,
        contactPhone: false,
        contactWechat: false,
        contactEmail: false,
        contactOther: false,
        contactAddress: false,
        remark: false,

        type: false,
        waitPair: false,
        wantVisit: false
      },

      rules: {
        name: [{ required: true, message: '不能为空' }],
        gender: [{ required: true, message: '不能为空' }],
        nickname: [{ required: true, message: '不能为空' }],
        anonymous: [{ required: true, message: '不能为空' }],
        area: [{ required: true, message: '不能为空' }],
        birthday: [{ required: true, message: '不能为空' }],

        type: [{ required: true, message: '不能为空' }],
        waitPair: [{ required: true, message: '不能为空' }],
        wantVisit: [{ required: true, message: '不能为空' }]

      }

    }
  },

  computed: {
    headerStyle() {
      // return {
      //   'border': '1px solid #ebeef5',
      //   'border-bottom': '0px solid #ebeef5'
      // }
      return {
        'border': '0px solid #ebeef5',
        'border-bottom': '1px solid #ebeef5'
      }
    },
    dataBankAccountTitle() {
      if (this.isAddBankAccount) {
        return '添加银行卡'
      } else {
        return '修改银行卡'
      }
    }
  },
  watch: {
    id() {
      this.getInfo()
    }
  },
  created() {
    this.getPublicData()
    this.getInfo()
  },
  methods: {
    getPublicData() {
      this.getGenders()
    },
    getGenders() {
      getGenders().then((res) => {
        this.genders = res.data
        for (const item of res.data) {
          genderKeyValue[item.code] = item.name
        }
      })
    },
    handleReturn() {
      this.$emit('backClick')
    },

    handleSave() {

    },
    handleEditAttr(name) {
      this.formEdit[name] = true
    },
    handleEditAttrOk(name) {
      this.$refs['form_' + name].validate((valid) => {
        if (valid) {
          this.formEdit[name] = false
          const postData = {
            id: this.form.id
          }
          if (name === 'birthday') {
            this.form[name] = parseTime(this.form[name], '{y}-{m}-{d}')
          }
          if (name === 'name') {
            postData.anonymous = this.form.anonymous
          }
          postData[name] = this.form[name]
          update(postData).then(res => {
            this.$notify({
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
          this.formSource[name] = this.form[name]
          if (name === 'name') {
            this.formSource.anonymous = this.form.anonymous
          }
        }
      })
    },
    handleEditAttrCancel(name) {
      this.form[name] = this.formSource[name]
      if (name === 'name') {
        this.form.anonymous = this.formSource.anonymous
      }
      this.formEdit[name] = false
    },
    getLines(str) {
      return getLines(str)
    },
    getInfo() {
      if (this.id) {
        getInfo(this.id).then(res => {
          this.formSource = res.data
          this.form = JSON.parse(JSON.stringify(res.data))

          for (const item in res.data) {
            this.formEdit[item] = false
          }
        })
      }
    }

  }
}

</script>

<style lang="scss" scoped>

  .datatable-header{
      height: 40px;
      width: 100%;
      background: white;
      padding: 12px 16px;
      /* border: 1px solid #ebeef5;
      border-bottom: 1px solid #ebeef5; */
      line-height: 1;
      .header-text{
        font-size: 18px;
        font-weight: bold;
        color:#303133;;
      }
  }
</style>

